package com.marketingexpert.controller.statemgnt;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.User;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.statreport.StatReportService;
import com.marketingexpert.service.usermgnt.UserService;

public class VisitCountInfoController implements Controller {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private static Logger logger = Logger.getLogger(VisitCountInfoController.class);
	
	private StatReportService statReportService;

	private UserService userService;
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("application/x-json;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		PrintWriter out = response.getWriter(); 
		
		String parentUserId = (String) request.getSession().getAttribute("parentUserId");		
		User parentUser = getUserService().queryUserByUId(parentUserId);	
		
		Calendar now = Calendar.getInstance();
		java.util.Date _endDate = now.getTime();
		String _end = sdf.format(_endDate);

		now.add(Calendar.DATE, -31);
		
		java.util.Date _startDate = now.getTime();
		String _start = sdf.format(_startDate);
		try {
		JSONObject visitInfo = getStatReportService().queryVisitCountInfo(parentUser.getId(),_start,_end);
		out.print(visitInfo);
		return null;
		} catch(MarketingExpertException e) {
			logger.error("com.marketingexpert.controller.statemgnt.VisitCountInfoController line 58", e);
			return null;
		}
	}

	public StatReportService getStatReportService() {
		return statReportService;
	}

	public void setStatReportService(StatReportService statReportService) {
		this.statReportService = statReportService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
